const pageSize = 15;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    empty: false,
    loadmore: false,
    nomore: false,
    list: [],
    modal: false,
    modalContent: "",
    popupShow: false,
    regionCode: [],
    regionCodeComplete: false
  },

  onLoad: function (options) {
    this.equipment_id = options.equipment_id;
    this.page = 1;
    this.getRegionCode();
    this.getPartnerRegionCode();
  },

  onPullDownRefresh: function () {
    this.refresh = true;
    this.reloadData();
  },

  reloadData() {
    this.page = 1;
    this.setData({
      list: [],
      loadmore: false,
      nomore: false
    })
    this.getRegionCode();
  },


  getPartnerRegionCode() {
    app.ajax({
      url: "Common/Equipment/getParterAreaCode",
      data: {
        equipment_id: this.equipment_id
      },
      success: res => {
        if (res.data.code > 0) {
          this.setData({
            regionCodeComplete: true,
            regionCode: res.data.data
          })
        } else {
          this.setData({
            regionCodeComplete: true,
            regionCode: []
          })
          // app.showToast(res.data.message)
        }
      },
      error: e => {
        app.showToast("网络错误");
      }
    })
  },


  //获取设备下发的区域码
  getRegionCode() {
    app.ajax({
      url: "Common/Equipment/getEquipmentAreaCode",
      data: {
        page: this.page,
        pageSize,
        equipment_id: this.equipment_id,
      },
      success: res => {
        if (this.refresh) {
          wx.stopPullDownRefresh();
          delete(this.refresh);
        }
        if (res.data.code > 0) {
          const source = res.data.data.list;
          let count = res.data.data.count;
          let list = this.data.list.concat(source);
          this.setData({
            list,
            empty: false,
            nomore: list.length >= count ? true : false,
            loadmore: list.length < count ? true : false
          })
        } else {
          let empty = false;
          let loadmore = false,
            nomore = false
          if (this.page == 1) {
            empty = true
          } else {
            nomore = true
          }
          this.setData({
            empty,
            loadmore,
            nomore
          })
        }
      },
      error: e => {
        if (this.refresh) {
          wx.stopPullDownRefresh();
          delete(this.refresh);
        }
        app.showToast("网络错误")
      }
    })
  },

  compatibleIssue() {
    wx.navigateTo({
      url: '../compatible/compatible?equipment_id=' + this.equipment_id,
    })
  },

  onReachBottom: function () {
    if (!this.data.nomore) {
      this.page = this.page + 1;
      this.getRegionCode();
    }
  },


  chooseRegionCode(e) {
    this.selectIndex = e.currentTarget.dataset.index;
    this.setData({
      modal: true,
      modalContent: `是否下发[${this.data.regionCode[this.selectIndex].name}]该区域码?`,
      popupShow: false
    })
  },

  handleModalClick(e) {
    let index = e.detail.index;
    if (index == 0) {
      this.setData({
        modal: false
      })
      delete(this.selectIndex)
    } else {
      wx.showLoading({
        mask: true,
      })
      app.ajax({
        url: "Common/Equipment/setAreaCode",
        data: {
          type: 1,
          area_code_id: this.data.regionCode[this.selectIndex].id,
          equipment_id: this.equipment_id
        },
        success: res => {
          wx.hideLoading();
          if (res.data.code > 0) {
            app.showToast(res.data.message, "success");
            this.setData({
              modal: false
            })
            delete(this.selectIndex)
            setTimeout(() => {
              this.reloadData();
            }, 1500)
          } else {
            this.setData({
              modal: false
            })
            delete(this.selectIndex)
            app.showToast(res.data.message, "none", 3000)
          }
        },
        error: e => {
          wx.hideLoading()
          app.showToast("网络错误")
        }
      })
    }
  },


  showPopup() {
    if (this.data.regionCode.length == 0 && this.data.regionCodeComplete) {
      app.showToast("暂无可以下发的区域码");
      return false;
    }
    this.setData({
      popupShow: true
    })
  },

  hidePopup() {
    this.setData({
      popupShow: false
    })
  }

})